package com.edu.hre.struts.action.pim;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.ExcelProcessor;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.struts.form.pim.KpiListImportForm;

public class KpiListImportAction extends Action {

	private static final Logger logger = Logger.getLogger(KpiListImportAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "kpiListImport";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		
		KpiListImportForm beanForm = (KpiListImportForm) form;
		
		if(request.getParameter("btnUpdate") == null){
			return mapping.findForward(ACTION_NAME);
		}
		
		FormFile myFile = beanForm.getWorkBook();
		int numOfRows = ExcelProcessor.importEmployeeKpi(myFile.getInputStream());
		
		if (numOfRows <= 0) {
			beanForm.setMessage("No rows inserted! Please check your file if you think there is something wrong!");
		} else {
			beanForm.setMessage(numOfRows + " row(s) have been inserted into the database!");
		}

		return mapping.findForward(ACTION_NAME);
	}
}
